package com.ruoyi.web.controller.install;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.install.DevServer;
import com.ruoyi.system.service.install.IDevServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服务器Controller
 * 
 * @author ruoyi
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/install/server")
public class DevServerController extends BaseController
{
    @Autowired
    private IDevServerService devServerService;

    /**
     * 查询服务器列表
     */
    @PreAuthorize("@ss.hasPermi('install:server:list')")
    @GetMapping("/list")
    public TableDataInfo list(DevServer devServer)
    {
        startPage();
        List<DevServer> list = devServerService.selectDevServerList(devServer);
        return getDataTable(list);
    }

    /**
     * 导出服务器列表
     */
    @PreAuthorize("@ss.hasPermi('install:server:export')")
    @Log(title = "导出服务器列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DevServer devServer)
    {
        List<DevServer> list = devServerService.selectDevServerList(devServer);
        ExcelUtil<DevServer> util = new ExcelUtil<DevServer>(DevServer.class);
        util.exportExcel(response, list, "服务器数据");
    }

    /**
     * 获取服务器详细信息
     */
    @PreAuthorize("@ss.hasPermi('install:server:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(devServerService.selectDevServerById(id));
    }

    /**
     * 新增服务器
     */
    @PreAuthorize("@ss.hasPermi('install:server:add')")
    @Log(title = "新增服务器", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DevServer devServer)
    {
        return toAjax(devServerService.insertDevServer(devServer));
    }

    /**
     * 修改服务器
     */
    @PreAuthorize("@ss.hasPermi('install:server:edit')")
    @Log(title = "修改服务器", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DevServer devServer)
    {
        return toAjax(devServerService.updateDevServer(devServer));
    }

    /**
     * 删除服务器
     */
    @PreAuthorize("@ss.hasPermi('install:server:remove')")
    @Log(title = "删除服务器", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(devServerService.deleteDevServerByIds(ids));
    }

    /**
     * 查看服务器命令
     */
    @PreAuthorize("@ss.hasPermi('install:service:edit')")
    @Log(title = "查看服务器命令", businessType = BusinessType.UPDATE)
    @GetMapping("/dockerPs/{id}")
    public AjaxResult dockerPs(@PathVariable("id") Long id)
    {
        return devServerService.command(id,"docker ps -a");
    }

    @PreAuthorize("@ss.hasPermi('install:service:edit')")
    @Log(title = "重启job服务", businessType = BusinessType.UPDATE)
    @GetMapping("/restart/{id}")
    public AjaxResult restartService(@PathVariable("id") Long id)
    {
        return devServerService.command(id,"docker restart fox_seven_job");
    }

    @PreAuthorize("@ss.hasPermi('install:service:edit')")
    @Log(title = "导出nacos库", businessType = BusinessType.EXPORT)
    @GetMapping("/exportDb/{id}")
    public AjaxResult exportDb(@PathVariable("id") Long id)
    {
        return devServerService.command(id,"mysqldump -hlocalhost -uroot -p7ysVgjyzQ5gd --single-transaction -P 3306 nacos -R --hex-blob > /root/yyzb/mysql/nacos"+ DateUtils.dateTime() +".sql");
    }

    @PreAuthorize("@ss.hasPermi('install:service:edit')")
    @Log(title = "导出mongo库集合", businessType = BusinessType.EXPORT)
    @GetMapping("/exportMongoDb/{id}")
    public AjaxResult exportMongoDb(@PathVariable("id") Long id)
    {
        ///usr/bin/mongodump --host localhost --port 27017 --username root --password 123456 --authenticationDatabase admin --db taxes  --out /root/yyzb/mongodb
        String folder = "/root/yyzb/mongodb";
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("mongoexport -utaxes-hyx -ptaxes-hyx --db=taxes --collection=ali_pay_config --out="+folder+"/ali_pay_config"+ DateUtils.dateTime() +".json");
        stringBuffer.append(" && mongoexport -utaxes-hyx -ptaxes-hyx --db=taxes --collection=lianlian_pay_config --out="+folder+"/lianlian_pay_config"+ DateUtils.dateTime() +".json");
        stringBuffer.append(" && mongoexport -utaxes-hyx -ptaxes-hyx --db=taxes --collection=newPay_config --out="+folder+"/newPay_config"+ DateUtils.dateTime() +".json");
        return devServerService.command(id,stringBuffer.toString());
    }
}
